package co.fitstart.entity.courseware.movement;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.principal.Principal;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name="movement")
@DynamicUpdate
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Movement {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 动作名称 */
    private String name;
    
    /** 动作简略图 */
    private String cover;
    
    /** 动作视频地址 */
    private String videoLink;
    
    /** 动作要领 */
    @Type(type = "text")
    private String summary;
    
    /** 动作类别 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private MovementCategory category;
    
    /** 动作器材 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private MovementEquipment equipment;
    
    /** 是否线上 */
    private boolean online;
    
    /** 动作创建时间 */
    private Date createTime;
    
    /** 动作更新时间 */
    private Date updateTime;
    
    /** 创建人 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Principal creator;
    
    @Column(name = "is_disabled")
    @JSONField(serialize = false)
    private boolean disabled;
    
    /** 组数 */
    private Integer groupAmount;
    
    /** 每组动作次数 */
    private Integer movementNumber;
    
    /** 最大重量 */
    private String maxWeight;
    
    /** 计时 */
    private String time;
    
    /**
     * The default constructor
     */
    public Movement() {
    }
    
    public Movement(String name,  boolean online) {
        this.name = name;
        this.online = online;
    }
    
    /**
     * @return
     */
    public Movement initialize() {
        this.createTime = new Date();
        this.updateTime = this.createTime;
        this.online = false;
        return this;
    }
    
    
    public Movement addMovementNumber(Integer groupAmount,Integer movementNumber,String maxWeight,String time) {
       
    	this.groupAmount = groupAmount;
    	this.movementNumber = movementNumber;
    	this.maxWeight = maxWeight;
    	this.time = time;
        return this;
    }
    
    /**
     * 
     * @param principal
     * @return
     */
    public Movement initialize(Principal principal) {
        this.createTime = new Date();
        this.updateTime = this.createTime;
        this.creator = principal;
        this.disabled = false;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Movement disable() {
        this.disabled = true;
        return this;
    }
    
    /**
     * @return
     */
    public Movement edit() {
        this.updateTime=new Date();
        return this;
    }

    /**
     * 
     * @param videoLink
     * @return
     */
    public Movement editVideoLink(String videoLink) {
        this.videoLink = videoLink;
        this.updateTime=new Date();
        return this;
    }
    
    /**
     * @param cover
     * @return
     */
    public Movement editCover(String cover) {
        this.cover = cover;
        this.updateTime=new Date();
        return this;
    }
    
    /**
     * 
     * @param cloudPath
     * @return
     */
    @Transient
    public String getVideo(String cloudPath) {
        return StringUtils.replace(videoLink, cloudPath + "/", "");
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the cover
     */
    public String getCover() {
        return cover;
    }

    /**
     * @return the videoLink
     */
    public String getVideoLink() {
        return videoLink;
    }

    /**
     * @return the summary
     */
    public String getSummary() {
        return summary;
    }

    /**
     * @return the category
     */
    public MovementCategory getCategory() {
        return category;
    }

    /**
     * @return the equipment
     */
    public MovementEquipment getEquipment() {
        return equipment;
    }

    /**
     * @return the online
     */
    public boolean isOnline() {
        return online;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the updateTime
     */
    public Date getUpdateTime() {
        return updateTime;
    }

    /**
     * @return the creator
     */
    public Principal getCreator() {
        return creator;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param cover the cover to set
     */
    public void setCover(String cover) {
        this.cover = cover;
    }

    /**
     * @param videoLink the videoLink to set
     */
    public void setVideoLink(String videoLink) {
        this.videoLink = videoLink;
    }

    /**
     * @param summary the summary to set
     */
    public void setSummary(String summary) {
        this.summary = summary;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(MovementCategory category) {
        this.category = category;
    }

    /**
     * @param equipment the equipment to set
     */
    public void setEquipment(MovementEquipment equipment) {
        this.equipment = equipment;
    }

    /**
     * @param online the online to set
     */
    public void setOnline(boolean online) {
        this.online = online;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param updateTime the updateTime to set
     */
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * @param creator the creator to set
     */
    public void setCreator(Principal creator) {
        this.creator = creator;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

	public int getGroupAmount() {
		return groupAmount;
	}

	public void setGroupAmount(int groupAmount) {
		this.groupAmount = groupAmount;
	}

	public int getMovementNumber() {
		return movementNumber;
	}

	public void setMovementNumber(int movementNumber) {
		this.movementNumber = movementNumber;
	}

	public String getMaxWeight() {
		return maxWeight;
	}

	public void setMaxWeight(String maxWeight) {
		this.maxWeight = maxWeight;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
    
    
    
}
